﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Wizard.UI;
using System.IO;
using System.Text.RegularExpressions;

namespace DVRMS_Renamer
{
    public partial class wizAutoPage1 : Wizard.UI.InternalWizardPage
    {
        public wizAutoPage1()
        {
            InitializeComponent();
        }

        public void StartProcessing()
        {
            ListView lstItems = new ListView();
            
            FormLayout(Globals.FormLayouts.flProgress);
            SetWizardButtons(WizardButtons.None);
            this.Refresh();

            // Find all the DVR-MS files and get their metadata (wizPage3)
            Common.LoadDVRMSFiles(this, pgrScanningFiles, lblScanningFiles, lstItems);

            Globals.selectedFilesToScan.Clear();

            for (int i = 0; i < lstItems.Items.Count; i++)
            {
                if (lstItems.Items[i].Checked)
                {
                    File theFile = (File)lstItems.Items[i].Tag;
                    Globals.selectedFilesToScan.Add(theFile);
                }
            }

            // Now gather the data from thetvdb.com (wizPage4)
            Common.ProcessItems(this, pgrMatching, lblMatching, lstItems);

            Globals.selectedFilesToRename.Clear();

            for (int i = 0; i < lstItems.Items.Count; i++)
            {
                if (lstItems.Items[i].Checked)
                {
                    File theFile = (File)lstItems.Items[i].Tag;
                    Globals.selectedFilesToRename.Add(theFile);
                }
            }

            // Now check for duplicates (optional) (wizPage6)
            if (Globals.selectedCompareFolder != null)
            {
                Common.CheckForDuplicates(this, pgrFindingDuplicates, lblFindingDuplicates, lstItems);

                Globals.selectedFilesToDelete.Clear();

                for (int i = 0; i < lstItems.Items.Count; i++)
                {
                    if (lstItems.Items[i].Checked)
                    {
                        File theFile = (File)lstItems.Items[i].Tag;
                        Globals.selectedFilesToDelete.Add(theFile);
                    }
                }
            }

            FormLayout(Globals.FormLayouts.flStandard);

            SetWizardButtons(WizardButtons.Next);
        }

        private void wizAutoPage1_WizardNext(object sender, WizardPageEventArgs e)
        {
            // TODO:DS Process the actions
        }

        private void wizAutoPage1_Load(object sender, EventArgs e)
        {
            lblScanningFiles.Text = "Working...";
            lblMatching.Text = "Waiting...";
            lblFindingDuplicates.Text = "Waiting...";
        }

        private void FormLayout(Globals.FormLayouts fl)
        {
            lblScanningFiles.Visible = (fl == Globals.FormLayouts.flProgress);
            pgrScanningFiles.Visible = (fl == Globals.FormLayouts.flProgress);
            lblMatching.Visible = (fl == Globals.FormLayouts.flProgress);
            pgrMatching.Visible = (fl == Globals.FormLayouts.flProgress);
            lblFindingDuplicates.Visible = (fl == Globals.FormLayouts.flProgress && Globals.selectedCompareFolder != null);
            pgrFindingDuplicates.Visible = (fl == Globals.FormLayouts.flProgress && Globals.selectedCompareFolder != null);
            txtReview.Visible = (fl == Globals.FormLayouts.flStandard);

            if (fl == Globals.FormLayouts.flProgress)
            {
                Banner.Title = "Processing";
                Banner.Subtitle = "Scanning your folder for DVR-MS files reading and matching the details from the files";
            }
            else
            {
                Banner.Title = "Review";
                Banner.Subtitle = "Review the changes that will be performed";
            }
        }
    }
}
